package com.lry.basic.redis;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/8/6
 */
public class BioServer {
    public static void main(String[] args) throws IOException {
        final ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(9999));
        System.out.println("server start...");
        System.out.println("wait for connect...");
        while(true){
            final Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("connect success,客户端ip:"+socket.getInetAddress()+" port:"+socket.getPort());
                        InputStream ins = socket.getInputStream();
                        byte[]  bytes = new byte[1024];
                        while(true){
                            System.out.println("wait for client send data...");
                            ins.read(bytes);
                            System.out.println(new String(bytes));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
